<?php

namespace app\mobile\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;
use think\Config;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
		$adv_id=Db::name('guanggaotype')->where(['type'=>'1','name'=>'首页开屏广告'])->column('id');

		$adv=Db::name('guanggao')->where([
			'type_data'=>'1',
			'guanggaotype_id'=>(string)$adv_id[0],
			'endtime'=>['>',time()]
		])->orderRaw('rand()')->find();

		$this->assign('adv',$adv);
		
        return $this->view->fetch();
    }
	
	public function home(){
		//轮播图
		$banner_type_id=Db::name('bannertype')->where(['type'=>1,'name'=>'首页'])->column('id');

		$rs_banner=Db::name('banner')->where([
			'type_data'=>1,
			'bannertype_id'=>$banner_type_id[0],
			/* 'endtime'=>['>',time()], */
		])->order('weigh desc')->select();

		$this->assign('rs_banner',$rs_banner);
		//行业信息
		$hangyexinxitype=Db::name('hangyexinxitype')->where([
			'show_switch'=>1,
			'pcindex_switch'=>1,
		])->order('weigh desc')->select();
		foreach($hangyexinxitype as $k=>$v){
			$hangyexinxitype[$k]['list']=Db::name('hangyexinxi')->where(['type_id'=>$v['id']])->order('weigh desc')->limit(2)->select();
		}
		$this->assign('hangyexinxitype',$hangyexinxitype);
		
		//期刊
		$rs_qikan=Db::name('qikan')->where(['show_switch'=>1])->order('weigh desc,updatetime desc')->limit(2)->select();
		$this->assign('rs_qikan',$rs_qikan);
		
		$rs_chuangxin_product=Db::name('product')->alias('p')->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')->where(['p.status'=>'2','biaoji'=>'2'])->limit(2)->select();
		$rs_youzhi_product=Db::name('product')->alias('p')->join('qiye q','p.qiye_id=q.id','LEFT')->field('p.*,q.name as qiye_name')->where(['p.status'=>'2','biaoji'=>'1'])->limit(2)->select();
		$this->assign('rs_chuangxin_product',$rs_chuangxin_product);
		$this->assign('rs_youzhi_product',$rs_youzhi_product);
		return $this->view->fetch();
	}
    
    public function single(Request $request){
        $article=$request->param('article/s','');
        $site=Config::get("site");
        switch($article){
            case 'aboutus':
                $title='关于我们';
                $content=$site['aboutus'];
            break;
            case 'guangxuanhezuo':
                $title='广宣合作';
                $content=$site['guangxuanhezuo'];
            break;
            default:
                $title='关于我们';
                $content=$site['aboutus'];
            break;    
                
            
        }
        $this->assign('title',$title);
        $this->assign('content',$content);
		return $this->view->fetch();
    }
    
    public function search(Request $request){
        $keyword=$request->param('keyword/s','');
        
        $result=[];
        $rs=Db::name('hangyexinxi')->field('a.*,t.name as type_name')->alias('a')->join('hangyexinxitype t','t.id=a.type_id','LEFT')->where(['a.title'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            $result['hangyexinxi']=$rs;
        }
        
        $rs=Db::name('biaozhunbianzhi')->field('a.*,t.name as type_name')->alias('a')->join('biaozhuntype t','t.id=a.biaozhuntype_id','LEFT')->where(['a.title'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            $result['biaozhunbianzhi']=$rs;
        }
        
        $rs=Db::name('huiyipeixun')->field('a.*,t.name as type_name')->alias('a')->join('huiyipeixuntype t','t.id=a.huiyipeixuntype_id','LEFT')->where(['a.name'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            $result['huiyipeixun']=$rs;
        }
        
        $rs=Db::name('hangyezhanhui')->field('a.*,t.name as type_name')->alias('a')->join('hangyezhanhuitype t','t.id=a.hangyezhanhuitype_id','LEFT')->where(['a.name'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            $result['hangyezhanhui']=$rs;
        }
        
        $rs=Db::name('renwuzhuanlan')->field('a.*,t.name as type_name')->alias('a')->join('renwuzhuanlantype t','t.id=a.renwuzhuanlantype_id','LEFT')->where(['a.title'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            $result['renwuzhuanlan']=$rs;
        }
        
        $rs=Db::name('qiye')->where(['name'=>['like',"%$keyword%"]])->select();;
        if(!empty($rs)){
            $result['qiye']=$rs;
        }
        
        $rs=Db::name('product')->field('p.*,q.name as qiye_name')->alias('p')->join('qiye q','q.id=p.qiye_id','LEFT')->where(['p.name'=>['like',"%$keyword%"]])->select();
        if(!empty($rs)){
            
            $result['product']=$rs;
        }
        
        $rs=Db::name('qikanwenzhang')->field('a.*,t.name as type_name')->alias('a')->join('qikanwenzhangtype t','t.id=a.qikanwenzhangtype_id','LEFT')->where(['a.title'=>['like',"%$keyword%"]])->order('qikanwenzhangtype_id desc')->select();
        if(!empty($rs)){
            $result['shukan']=$rs;
        }
        
        $this->assign('result',$result);
        //var_dump($result);exit;
        $this->assign('keyword',$keyword);
        return $this->view->fetch();
    }
}
